package com.qf.office.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.office.admin.common.qo.CandidateDetailsQo;
import com.qf.office.admin.common.vo.*;
import com.qf.office.admin.entity.*;
import com.qf.office.admin.mapper.*;
import com.qf.office.admin.service.CandidateService;
import com.qf.office.common.base.ex.ServiceException;
import com.qf.office.common.base.result.ResultStatus;
import com.qf.office.common.base.utils.CommonBeanutils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @className:CandidateServiceImpl
 * @author:廖广军
 * @date:2024/1/23 19:20
 * @version:1.0
 */
@Service
public class CandidateServiceImpl implements CandidateService {
    @Resource
    private BasicInformationMapper basicInformationMapper;
    @Resource
    private CandidateMapper candidateMapper;
    @Resource
    private EducationMapper educationMapper;
    @Resource
    private JobIntentionMapper jobIntentionMapper;
    @Resource
    private ProjectMapper projectMapper;
    @Resource
    private SkillMapper skillMapper;
    @Resource
    private WorkExperienceMapper workExperienceMapper;
    @Resource
    private CandidateDetailsMapper candidateDetailsMapper;


    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public int addCandidateDetails(CandidateDetailsQo candidateDetailsQo) {
        int count = -1;

        BasicInformation basicInformation = CommonBeanutils.covertTo(candidateDetailsQo.getBasicInformationQo(), BasicInformation::new);
        count = basicInformationMapper.insert(basicInformation);

        Candidate candidate = CommonBeanutils.covertTo(candidateDetailsQo.getCandidateQo(), Candidate::new);
        count = candidateMapper.insert(candidate);

        Education education = CommonBeanutils.covertTo(candidateDetailsQo.getEducationQo(), Education::new);
        count = educationMapper.insert(education);

        JobIntention jobIntention = CommonBeanutils.covertTo(candidateDetailsQo.getJobIntentionQo(), JobIntention::new);
        count = jobIntentionMapper.insert(jobIntention);

        Project project = CommonBeanutils.covertTo(candidateDetailsQo.getProjectQo(), Project::new);
        count = projectMapper.insert(project);

        Skill skill = CommonBeanutils.covertTo(candidateDetailsQo.getSkillQo(), Skill::new);
        count = skillMapper.insert(skill);

        WorkExperience workExperience = CommonBeanutils.covertTo(candidateDetailsQo.getWorkExperienceQo(), WorkExperience::new);
        count = workExperienceMapper.insert(workExperience);
        if (count <= 0) {
            throw new ServiceException(ResultStatus.INSERT_FAULT);
        }
        return count;
    }

    @Override
    public CandidateDetailsVo getCandidateDetails(Integer candidateId) {
        CandidateDetailsVo candidateDetailsVo = new CandidateDetailsVo();
        //通过候选人id查找基础信息
        QueryWrapper<BasicInformation> qw01 = new QueryWrapper<>();
        qw01.lambda().eq(BasicInformation::getCandidateId, candidateId);
        BasicInformation basicInformation = basicInformationMapper.selectOne(qw01);
        BasicInformationVo basicInformationVo = CommonBeanutils.covertTo(basicInformation, BasicInformationVo::new);

        //通过候选人id查找求职意向
        QueryWrapper<JobIntention> qw02 = new QueryWrapper<>();
        qw02.lambda().eq(JobIntention::getCandidateId, candidateId);
        JobIntention jobIntention = jobIntentionMapper.selectOne(qw02);
        JobIntentionVo jobIntentionVo = CommonBeanutils.covertTo(jobIntention, JobIntentionVo::new);

        //通过候选人id查找工作经历
        QueryWrapper<WorkExperience> qw03 = new QueryWrapper<>();
        qw03.lambda().eq(WorkExperience::getCandidateId, candidateId);
        WorkExperience workExperience = workExperienceMapper.selectOne(qw03);
        WorkExperienceVo workExperienceVo = CommonBeanutils.covertTo(workExperience, WorkExperienceVo::new);

        //通过候选人id查找教育经历
        QueryWrapper<Education> qw04 = new QueryWrapper<>();
        qw04.lambda().eq(Education::getCandidateId, candidateId);
        Education education = educationMapper.selectOne(qw04);
        EducationVo educationVo = CommonBeanutils.covertTo(education, EducationVo::new);

        candidateDetailsVo.setBasicInformationVo(basicInformationVo);
        candidateDetailsVo.setJobIntentionVo(jobIntentionVo);
        candidateDetailsVo.setWorkExperienceVo(workExperienceVo);
        candidateDetailsVo.setEducationVo(educationVo);
        return candidateDetailsVo;
    }

    @Override
    public List<Map<String,Integer>>  getCandidateCount() {
        List<Map<String, Integer>> maps = candidateDetailsMapper.selectCandidateCount();
        if (ObjectUtils.isEmpty(maps)) {
            throw new ServiceException(ResultStatus.ORDERS_XX);
        }
        return maps;
    }
}
